package com.addplus.townmall.server.scheduled.scheduletask;

import com.addplus.townmall.server.api.mapper.town.*;
import com.addplus.townmall.server.api.model.town.OrderRefundApplicate;
import com.addplus.townmall.server.api.model.town.OrderRefundApplicationLog;
import com.addplus.townmall.server.api.modelenum.OrderRefundEnum;
import com.alibaba.dubbo.rpc.RpcContext;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * 类名: UpdateOrderRefundEnumTask
 *
 * @author dengrenning
 * @version V1.0
 * @date 2018/11/8 2:57 PM
 * @description 类描述: 订单售后状态定时器
 */
@Component
public class UpdateOrderRefundEnumTask {

    private Logger logger = LoggerFactory.getLogger(SpikeActiceStatusTask.class);

    @Autowired
    private OrderRefundApplicateMapper orderRefundApplicateMapper;
    @Autowired
    private OrderRefundApplicationLogMapper orderRefundApplicationLogMapper;

    /**
     * 方法描述：修改订单售后状态
     *
     * @throws Exception
     * @author zhangjiehang
     * @date 2018/11/8 3:00 PM
     */
    @Scheduled(cron = "0 */1 * * * ?")
    private void processUnStart() throws Exception {
        logger.info("修改订单售后状态任务开始");
        QueryWrapper<OrderRefundApplicate> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("order_refund_enum", "USER_AMOUNT_APPLICATION", "USER_AMOUNT_GOODS_APPLICATION", "USER_RETURN_CONFIRM_GOODS");
        queryWrapper.eq("is_delete", 0);
        queryWrapper.orderByAsc("gmt_modified");
        List<OrderRefundApplicate> spikeActives = orderRefundApplicateMapper.selectList(queryWrapper);
        if (spikeActives == null || spikeActives.isEmpty()) {
            logger.info("修改订单售后状态活动列表未空");
            return;
        } else {
            spikeActives.forEach(item -> {
                if (item.getOrderRefundEnum().getContext().equals("已提交退款申请")) {
                    Date date = new Date();
                    UpdateWrapper<OrderRefundApplicate> updateWrapper = new UpdateWrapper<>();
                    updateWrapper.eq("id", item.getId());
                    updateWrapper.set("order_refund_enum", "STORE_WAIT_AUDIT");
                    updateWrapper.set("refund_type", 0);
                    updateWrapper.set("seller_audit_status", 0);
                    updateWrapper.set("gmt_modified", date);
                    orderRefundApplicateMapper.update(new OrderRefundApplicate(), updateWrapper);

                    UpdateWrapper<OrderRefundApplicationLog> orderRefundApplicationLogUpdateWrapper = new UpdateWrapper<>();
                    orderRefundApplicationLogUpdateWrapper.eq("refund_Id", item.getId());
                    orderRefundApplicationLogUpdateWrapper.eq("step", item.getOrderRefundEnum());
                    orderRefundApplicationLogUpdateWrapper.set("log_status", 1);
                    orderRefundApplicationLogUpdateWrapper.set("end_date", date);
                    orderRefundApplicationLogUpdateWrapper.set("gmt_modified", date);
                    orderRefundApplicationLogMapper.update(new OrderRefundApplicationLog(), orderRefundApplicationLogUpdateWrapper);

                    OrderRefundApplicationLog orderRefundApplicateLog = new OrderRefundApplicationLog();
                    orderRefundApplicateLog.setRefundId(item.getId());
                    orderRefundApplicateLog.setOperateDate(new Date());
                    orderRefundApplicateLog.setOperatorId(new String("142"));
                    orderRefundApplicateLog.setOperateContent(OrderRefundEnum.STORE_WAIT_AUDIT.getContext());
                    orderRefundApplicateLog.setStep(OrderRefundEnum.STORE_WAIT_AUDIT);
                    orderRefundApplicateLog.setApplyNumber(1);
                    orderRefundApplicateLog.setLogStatus(0);
                    orderRefundApplicateLog.setIsDelete(0);
                    orderRefundApplicateLog.setGmtCreate(new Date());
                    orderRefundApplicateLog.setGmtModified(new Date());
                    orderRefundApplicationLogMapper.insert(orderRefundApplicateLog);
                }
                if (item.getOrderRefundEnum().getContext().equals("已提交退款退货申请")) {
                    Date date = new Date();
                    UpdateWrapper<OrderRefundApplicate> updateWrapper = new UpdateWrapper<>();
                    updateWrapper.eq("id", item.getId());
                    updateWrapper.set("order_refund_enum", "STORE_WAIT_AUDIT");
                    updateWrapper.set("refund_type", 1);
                    updateWrapper.set("seller_audit_status", 0);
                    updateWrapper.set("gmt_modified", date);
                    orderRefundApplicateMapper.update(new OrderRefundApplicate(), updateWrapper);

                    UpdateWrapper<OrderRefundApplicationLog> orderRefundApplicationLogUpdateWrapper = new UpdateWrapper<>();
                    orderRefundApplicationLogUpdateWrapper.eq("refund_Id", item.getId());
                    orderRefundApplicationLogUpdateWrapper.eq("step", item.getOrderRefundEnum());
                    orderRefundApplicationLogUpdateWrapper.set("log_status", 1);
                    orderRefundApplicationLogUpdateWrapper.set("end_date", date);
                    orderRefundApplicationLogUpdateWrapper.set("gmt_modified", date);
                    orderRefundApplicationLogMapper.update(new OrderRefundApplicationLog(), orderRefundApplicationLogUpdateWrapper);

                    OrderRefundApplicationLog orderRefundApplicateLog = new OrderRefundApplicationLog();
                    orderRefundApplicateLog.setRefundId(item.getId());
                    orderRefundApplicateLog.setOperateDate(new Date());
                    orderRefundApplicateLog.setOperatorId(new String("142"));
                    orderRefundApplicateLog.setOperateContent(OrderRefundEnum.STORE_WAIT_AUDIT.getContext());
                    orderRefundApplicateLog.setStep(OrderRefundEnum.STORE_WAIT_AUDIT);
                    orderRefundApplicateLog.setApplyNumber(1);
                    orderRefundApplicateLog.setLogStatus(0);
                    orderRefundApplicateLog.setIsDelete(0);
                    orderRefundApplicateLog.setGmtCreate(new Date());
                    orderRefundApplicateLog.setGmtModified(new Date());
                    orderRefundApplicationLogMapper.insert(orderRefundApplicateLog);
                }
                if (item.getOrderRefundEnum().getContext().equals("用户已寄回商品")) {
                    Date date = new Date();
                    UpdateWrapper<OrderRefundApplicate> updateWrapper = new UpdateWrapper<>();
                    updateWrapper.eq("id", item.getId());
                    updateWrapper.set("order_refund_enum", "STORE_RECEIVE_GOODS");
                    updateWrapper.set("gmt_modified", date);
                    orderRefundApplicateMapper.update(new OrderRefundApplicate(), updateWrapper);

                    UpdateWrapper<OrderRefundApplicationLog> orderRefundApplicationLogUpdateWrapper = new UpdateWrapper<>();
                    orderRefundApplicationLogUpdateWrapper.eq("refund_Id", item.getId());
                    orderRefundApplicationLogUpdateWrapper.eq("step", OrderRefundEnum.USER_RETURN_CONFIRM_GOODS);
                    orderRefundApplicationLogUpdateWrapper.set("log_status", 1);
                    orderRefundApplicationLogUpdateWrapper.set("end_date", date);
                    orderRefundApplicationLogUpdateWrapper.set("gmt_modified", date);
                    orderRefundApplicationLogMapper.update(new OrderRefundApplicationLog(), orderRefundApplicationLogUpdateWrapper);

                    OrderRefundApplicationLog orderRefundApplicateLog = new OrderRefundApplicationLog();
                    orderRefundApplicateLog.setRefundId(item.getId());
                    orderRefundApplicateLog.setOperateDate(new Date());
                    orderRefundApplicateLog.setOperatorId(new String("142"));
                    orderRefundApplicateLog.setOperateContent(OrderRefundEnum.STORE_RECEIVE_GOODS.getContext());
                    orderRefundApplicateLog.setStep(OrderRefundEnum.STORE_RECEIVE_GOODS);
                    orderRefundApplicateLog.setApplyNumber(1);
                    orderRefundApplicateLog.setLogStatus(0);
                    orderRefundApplicateLog.setIsDelete(0);
                    orderRefundApplicateLog.setGmtCreate(new Date());
                    orderRefundApplicateLog.setGmtModified(new Date());
                    orderRefundApplicationLogMapper.insert(orderRefundApplicateLog);
                }
            });
        }
        logger.info("秒杀场次开始活动定时任务完成");
    }

}
